package com.xw.convert;

import android.util.Log;

public class dataConvert {
	
	
	
    /**
     * 
     * 
     * @param lng： 经度例如 108.901242
     * @return  10800+0.901242*60
     */
    public static String ConverLngForDf(String lng)
    {
	     float fltKldJd =  Float.parseFloat(lng);	     
	     int intJd = (int)fltKldJd;
	     float fltJdxs = fltKldJd - intJd;
	     float kldJd = (intJd*100)+(fltJdxs*60);
	     String StrFloatLng = String.valueOf(kldJd);
	
    	 return StrFloatLng;
    	
    }
    
    /**
     *
     * @param lng： 纬度例如 34.246624
     * @return  3400+0.246624*60
     */
    public static String ConverLatForDf(String lat)
    {
         float fltKldWd =  Float.parseFloat(lat);    
	     int intWd = (int)fltKldWd;
	     float fltWdxs = fltKldWd - intWd;
	     float kldWd = (intWd*100)+(fltWdxs*60);
	     String StrFloatLat = String.valueOf(kldWd);

	     return StrFloatLat;
    }

	//终端ID串转换 为 bytes字节数组
	public static byte[]  getTuidBytes(String strTuid)
	{
		while( strTuid.length() < 14 )
		{
			strTuid = strTuid+"f";
		}
		byte []tuid = dataConvert.hexStringToByte(strTuid);

		return tuid;
	}
    
	
	//"0102"---0x0102
	public static byte[] hexStringToByte(String hex) { 
	    int len = (hex.length() / 2); 
	    byte[] result = new byte[len]; 
	    char[] achar = hex.toCharArray(); 
	    for (int i = 0; i < len; i++) { 
	     int pos = i * 2; 
	     result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1])); 
	    } 
	    return result; 
	} 
	private static byte toByte(char c) { 
	    byte b = (byte) "0123456789abcdef".indexOf(c); 
	    return b; 
	}
	
	
	//字节数组转为16进制串，例如String="12"   则byte[0]=49，byte[1]="50",转为HexString="3132"
	public static String byteArrToHexStr(byte[] arr) {
		StringBuilder sb = new StringBuilder("");
		for (int i = 0; i < arr.length; i++) {
			int b = arr[i];
			int high = (arr[i] >> 4 & 0x0f);
			int low = arr[i] & 0x0f;
			System.out.println(high + "," + low);
			if (high >= 10)
				sb.append((char) (high - 10 + 'A'));
			else
				sb.append(high);
			if (low >= 10)
				sb.append((char) (low - 10 + 'A'));
			else
				sb.append(low);
		
		}
		return sb.toString();
	}
	
	
	
	// 连接2个byte数组
	public static byte[] join(byte[] a1, byte[] a2) {
		byte[] result = new byte[a1.length + a2.length];
		System.arraycopy(a1, 0, result, 0, a1.length);
		System.arraycopy(a2, 0, result, a1.length, a2.length);
		return result;
	}
	//short转byte[2],acs=true表示byte[0]存放高字节，例如0x00fd ,byte[0]=0x00; byte[1]=0xfd;
	public final static byte[] getBytes(short s, boolean asc) {
	    byte[] buf = new byte[2];
	    if (asc)      for (int i = buf.length - 1; i >= 0; i--) {        buf[i] = (byte) (s & 0x00ff);
	        s >>= 8;
	      }
	    else
	      for (int i = 0; i < buf.length; i++) {
	        buf[i] = (byte) (s & 0x00ff);
	        s >>= 8;
	      }
	    return buf;
	  }
	  public final static  byte[] getBytes(int s, boolean asc) {
	    byte[] buf = new byte[4];
	    if (asc)
	      for (int i = buf.length - 1; i >= 0; i--) {
	        buf[i] = (byte) (s & 0x000000ff);
	        s >>= 8;
	      }
	    else
	      for (int i = 0; i < buf.length; i++) {
	        buf[i] = (byte) (s & 0x000000ff);
	        s >>= 8;
	      }
	    return buf;
	  }
	  public final static byte[] getBytes(long s, boolean asc) {
	    byte[] buf = new byte[8];
	    if (asc)
	      for (int i = buf.length - 1; i >= 0; i--) {
	        buf[i] = (byte) (s & 0x00000000000000ff);
	        s >>= 8;
	      }
	    else
	      for (int i = 0; i < buf.length; i++) {
	        buf[i] = (byte) (s & 0x00000000000000ff);
	        s >>= 8;
	      }
	    return buf;
	  }
	  public final static short getShort(byte[] buf, boolean asc) {
	    if (buf == null) {
	      throw new IllegalArgumentException("byte array is null!");
	    }
	    if (buf.length > 2) {
	      throw new IllegalArgumentException("byte array size > 2 !");
	    }
	    short r = 0;
	    if (asc)
	      for (int i = buf.length - 1; i >= 0; i--) {
	        r <<= 8;
	        r |= (buf[i] & 0x00ff);
	      }
	    else
	      for (int i = 0; i < buf.length; i++) {
	        r <<= 8;
	        r |= (buf[i] & 0x00ff);
	      }
	    return r;
	  }
	  public final static int getInt(byte[] buf, boolean asc) {
	    if (buf == null) {
	      throw new IllegalArgumentException("byte array is null!");
	    }
	    if (buf.length > 4) {
	      throw new IllegalArgumentException("byte array size > 4 !");
	    }
	    int r = 0;
	    if (asc)
	      for (int i = buf.length - 1; i >= 0; i--) {
	        r <<= 8;
	        r |= (buf[i] & 0x000000ff);
	      }
	    else
	      for (int i = 0; i < buf.length; i++) {
	        r <<= 8;
	        r |= (buf[i] & 0x000000ff);
	      }
	    return r;
	  }
	  public final static long getLong(byte[] buf, boolean asc) {
	    if (buf == null) {
	      throw new IllegalArgumentException("byte array is null!");
	    }
	    if (buf.length > 8) {
	      throw new IllegalArgumentException("byte array size > 8 !");
	    }
	    long r = 0;
	    if (asc)
	      for (int i = buf.length - 1; i >= 0; i--) {
	        r <<= 8;
	        r |= (buf[i] & 0x00000000000000ff);
	      }
	    else
	      for (int i = 0; i < buf.length; i++) {
	        r <<= 8;
	        r |= (buf[i] & 0x00000000000000ff);
	      }
	    return r;
	  }
	  
	  
	  public static String getTwoChars(String strSrc)
	  {
		  
		  while(strSrc.length() < 2)
		  {
			  strSrc ="0"+strSrc;
		  }
		   return strSrc; 
	  }

}
